//% color="#AA278D" iconWidth=50 iconHeight=40
namespace ESP32S3AI {
     //% block="Wi-Fi 连接到 热点：[ssid]密码：[password]" blockType="command"
     //% ssid.shadow="string" ssid.defl="***"
     //% password.shadow="string" password.defl="***"
    export function wifibegin(parameter: any, block: any) {
        let ssid=parameter.ssid.code
        let password=parameter.password.code

        Generator.addInclude(`wifi`, `#include <WiFi.h>`);
        Generator.addSetup(`ssid`, `const char *ssid = ${ssid};`);
        Generator.addSetup(`password`, `const char *password = ${password};`);
        Generator.addSetup(`WiFibegin`, `WiFi.begin(ssid, password);`);
    }  
   //% block="WIFI连接成功" blockType="boolean"
    export function wifiyes(parameter: any, block: any) {
        Generator.addCode(`(WiFi.status() == WL_CONNECTED)`);

    }
   //% block="点亮板载LED灯" blockType="command"
    export function LED_open(parameter: any, block: any) {
        Generator.addInclude(`LED`, `int led = 3;`);
        Generator.addSetup(`pinMode`, `pinMode(led,OUTPUT);`);
        Generator.addCode(`digitalWrite(led,HIGH);`);

    }
   //% block="熄灭板载LED灯" blockType="command"
    export function LED_close(parameter: any, block: any) {
        Generator.addInclude(`LED`, `int led = 3;`);
        Generator.addSetup(`pinMode`, `pinMode(led,OUTPUT);`);
        Generator.addCode(`digitalWrite(led,LOW);`);
    }
  //% block="打开红外补光灯" blockType="command"
    export function IR_open(parameter: any, block: any) {
        Generator.addInclude(`IR`, `int IR = 47;`);
        Generator.addSetup(`pinModeIR`, `pinMode(IR,OUTPUT);`);
        Generator.addCode(`digitalWrite(IR,HIGH);`);

    }
   //% block="关闭红外补光灯" blockType="command"
    export function IR_close(parameter: any, block: any) {
        Generator.addInclude(`IR`, `int IR = 47;`);
        Generator.addSetup(`pinModeIR`, `pinMode(IR,OUTPUT);`);
        Generator.addCode(`digitalWrite(IR,LOW);`);
    }
    //% block="按键Boot按下？" blockType="boolean"
    export function boot_key(parameter: any, block: any) {
       
        Generator.addSetup(`pinModeboot`, `pinMode(0, INPUT);`);
        Generator.addCode(`!digitalRead(0)`);
    }
   //% block="SD卡初始化" blockType="command"
    export function SD_setup(parameter: any, block: any) {
        Generator.addInclude(`SPI`, `#include <SPI.h>\n#include "FS.h"\n#include <SD_MMC_yuntian.h>`);
        Generator.addInclude(`SD_const`, `const uint8_t SD_CMD = 11;\nconst uint8_t SD_CLK = 12;\nconst uint8_t SD_DATA0 = 13;`);
        Generator.addInclude(`SD_void`,`void writeFile(fs::FS &fs, const char * path, const char * message){
    File file = fs.open(path, FILE_WRITE);
    if(!file){
       return;
    }
    file.print(message);
}
String readFile(fs::FS &fs, const char * path){
    
    String temp="";
    String text="";
    File file = fs.open(path);
    if(!file){
        return "";
    }

   
    while(file.available()){
        temp=(char)file.read();
        text=text+ temp;
      }
    return text;
}`);
Generator.addSetup(`SD_MMCsetPins`, `SD_MMC.setPins(SD_CLK, SD_CMD, SD_DATA0);\n  SD_MMC.begin("/sdcard", true);`);
 }
//% block="存储[text]到SD卡文件[filename]" blockType="command"
     //% text.shadow="string" text.defl="hello"
     //% filename.shadow="string" filename.defl="/hello.txt"
    export function writefile(parameter: any, block: any) {
        let text=parameter.text.code
        let filename=parameter.filename.code
        Generator.addCode(`writeFile(SD_MMC, ${filename}, ${text});`);
    }  
//% block="读取SD卡文件[filename]" blockType="reporter"
    //% filename.shadow="string" filename.defl="/hello.txt"
    export function readfile(parameter: any, block: any) {
        let filename=parameter.filename.code
        Generator.addCode(`readFile(SD_MMC, ${filename})`);
    }  
}
